﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Simple.EF;
using System.Data.Entity;
using Simple.Entity.Models;
using Simple.Common.Infrastructure;
using System.Data.Entity.ModelConfiguration;
using Simple.Common.Infrastructure.DependencyManagement;

namespace Simple.Service
{
    public class WorkEfDbContext : EfDbContext
    {
        public DbSet<Sys_DataDictionary> Sys_DataDictionary { get; set; }
        public DbSet<Sys_LoginLog> Sys_LoginLog { get; set; }
        public DbSet<Sys_Menu> Sys_Menu { get; set; }
        public DbSet<Sys_Role> Sys_Role { get; set; }
        public DbSet<Sys_RoleForMenu> Sys_RoleForMenu { get; set; }
        public DbSet<Sys_User> Sys_User { get; set; }
        public DbSet<Sys_UserForMenu> Sys_UserForMenu { get; set; }
        public DbSet<Sys_ActivityLog> Sys_ActivityLog { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var typesToRegister = typeof(Sys_User).Assembly.GetTypes()
           .Where(type => !String.IsNullOrEmpty(type.Namespace))
           .Where(type => type.BaseType != null && type.BaseType.IsGenericType &&
               type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }

            base.OnModelCreating(modelBuilder);
        }
    }

}
